package com.qs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageInfo;
import com.qs.ConStants;
import com.qs.commen.Response;
import com.qs.pojo.SysOperator;
import com.qss.service.OperatorService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @user HP
 * @email 1460961774@qq.com
 * @Date 2021-10-08 15:55
 * @packageName com.qs.controller
 */@Controller
@RequestMapping("/opera")

public class OperatorController {
     @Reference
     private OperatorService service;

    @GetMapping("/toDate")
    public String toData(){
        return "opera/data";
    }
    @PostMapping("/data")
    @ResponseBody
    public Response data(SysOperator user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = ConStants.DEFAULTE_PAGE) Integer pageSize) throws JsonProcessingException {
        //创建日志对象 模块 用户 类型 分页查询用户当前登录的用户


        PageInfo<SysOperator> pageInfo = service.queryUsers(user, pageNumber, pageSize);
        return new Response(200,pageInfo);
    }

    /**
     * 跳转到编辑或新增页面
     * @param user
     * @param model
     * @return
     */
    @GetMapping("/toEdit")
    public String toEdit(SysOperator user, Model model){
        if(user!=null && user.getOperId()!=null){
            user = service.queryUserById(user.getOperId());
            model.addAttribute("opera",user);
        }

        return "opera/edit";
    }

    /**
     * 新增或更新用户信息
     * @param user
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Response save(SysOperator user){
        boolean status = service.saveUser(user);
        if(status){
            return Response.success("保存成功");
        }
        return new Response(400,"操作失败");
    }
    @DeleteMapping("/deletes")
    @ResponseBody
    public Response delete(Integer[] ids){

        boolean status = service.delUserById(ids);
        if(status) return Response.success("删除成功！");
        return Response.error();
    }

    @PostMapping("checkLoginName")
    @ResponseBody
    public  boolean checkLoginName(SysOperator user){

        SysOperator sysUser = service.queryUserByUserName(user.getModuleName());
        if (sysUser==null)return true;
        return false;
    }
}
